#define _CRT_SECURE_NO_WARNINGS

#include "Sort.h"
#include "Stack.h"

void PrintArray(int* a, int n) {
	for (int i = 0; i < n; i++) {
		printf("%d ", a[i]);
	}
	printf("\n");
}

void InsertSort(int* a, int n) {
	for (int i = 0; i < n - 1; i++) {
		int end = i;
		int key = a[end + 1];
		while (end >= 0 && a[end] > key) {
			a[end + 1] = a[end];
			--end;
		}
		a[end + 1] = key;
	}
}

void ShellSort(int* a, int n) {
	int gap = n;
	while (gap > 1) {
		gap = gap / 3 + 1;
		for (int i = 0; i < n - gap; i++) {
			int end = i;
			int key = a[end + gap];
			while (end >= 0 && a[end] > key) {
				a[end + gap] = a[end];
				end -= gap;
			}
			a[end + gap] = key;
		}
	}
}

void Swap(int* a, int* b) {
	int temp = *a;
	*a = *b;
	*b = temp;
}
void SelectSort(int* a, int n) {
	int begin = 0, end = n - 1;
	while (begin < end) {
		int mini = begin, maxi = begin;
		for (int i = begin; i <= end; i++) {
			if (a[i] < a[mini])
				mini = i;
			if (a[i] > a[maxi])
				maxi = i;
		}
		Swap(&a[mini], &a[begin]);
		if (maxi == begin)
			maxi = mini;
		Swap(&a[maxi], &a[end]);
		++begin;
		--end;
	}
}

void AdjustDown(int* a, int n, int parent) {
	int child = parent * 2 + 1;
	while (child < n) {
		if (child + 1 < n && a[child] < a[child + 1])
			++child;
		if (a[parent] < a[child]) {
			Swap(&a[parent], &a[child]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
			break;
	}
}
void HeapSort(int* a, int n) {
	for (int i = (n - 2) / 2; i >= 0; --i)
		AdjustDown(a, n, i);

	int end = n - 1;
	while (end > 0) {
		Swap(&a[0], &a[end]);
		AdjustDown(a, end, 0);
		--end;
	}
}

void BubbleSort(int* a, int n) {
	for (int j = 0; j < n - 1; ++j) {
		bool exchange = false;
		for (int i = 1; i < n - j; ++i) {
			if (a[i - 1] > a[i]) {
				Swap(&a[i - 1], &a[i]);
				exchange = true;
			}
		}
		if (exchange == false)
			break;
	}
}

//int PartSort(int* a, int left, int right) {
//	int keyi = left;
//	while (left < right) {
//		while (left < right && a[right] >= a[keyi])
//			--right;
//		while (left < right && a[left] <= a[keyi])
//			++left;
//		Swap(&a[left], &a[right]);
//	}
//	Swap(&a[keyi], &a[left]);
//	return left;
//}
//int PartSort2(int* a, int left, int right) {
//	int key = a[left];
//	int hole = left;
//	while (left < right) {
//		while (left < right && a[right] >= key)
//			--right;
//		a[hole] = a[right];
//		hole = right;
//		
//		while (left < right && a[left] <= key)
//			++left;
//		a[hole] = a[left];
//		hole = left;
//	}
//	a[hole] = key;
//	return hole;
//}
//int PartSort3(int* a, int left, int right) {
//	int keyi = left;
//	int prev = left;
//	int cur = prev + 1;
//	while (cur <= right) {
//		if (a[cur] < a[keyi] && ++prev != cur)
//			Swap(&a[prev], &a[cur]);
//		++cur;
//	}
//	Swap(&a[prev], &a[keyi]);
//	keyi = prev;
//	return keyi;
//}

int GetMidIndex(int* a, int left, int right) {
	int mid = (left + right) / 2;

	if (a[left] < a[mid]) {
		if (a[mid] < a[right])
			return mid;
		else if (a[left] < a[right])
			return right;
		else
			return left;
	}
	else { //a[left] > a[mid]
		if (a[mid] > a[right])
			return mid;
		else if (a[left] > a[right])
			return right;
		else
			return left;
	}
}
int PartSort(int* a, int left, int right) {
	int midi = GetMidIndex(a, left, right);
	Swap(&a[left], &a[midi]);
	int keyi = left;
	while (left < right) {
		while (left < right && a[right] >= a[keyi])
			--right;
		while (left < right && a[left] <= a[keyi])
			++left;
		Swap(&a[left], &a[right]);
	}
	Swap(&a[keyi], &a[left]);
	return left;
}
int PartSort2(int* a, int left, int right) {
	int midi = GetMidIndex(a, left, right);
	Swap(&a[left], &a[midi]);
	int key = a[left];
	int hole = left;
	while (left < right) {
		while (left < right && a[right] >= key)
			--right;
		a[hole] = a[right];
		hole = right;

		while (left < right && a[left] <= key)
			++left;
		a[hole] = a[left];
		hole = left;
	}
	a[hole] = key;
	return hole;
}
int PartSort3(int* a, int left, int right) {
	int midi = GetMidIndex(a, left, right);
	Swap(&a[left], &a[midi]);
	int keyi = left;
	int prev = left;
	int cur = prev + 1;
	while (cur <= right) {
		if (a[cur] < a[keyi] && ++prev != cur)
			Swap(&a[prev], &a[cur]);
		++cur;
	}
	Swap(&a[prev], &a[keyi]);
	keyi = prev;
	return keyi;
}
//void QuickSort(int* a, int begin, int end) {
//	if (begin >= end)
//		return;
//	int keyi = PartSort(a, begin, end);
//	QuickSort(a, begin, keyi - 1);
//	QuickSort(a, keyi + 1, end);
//}
void QuickSort(int* a, int begin, int end) {
	if (begin >= end)
		return;

	int left = begin, right = end;
	int midi = GetMidIndex(a, left, right);
	Swap(&a[left], &a[midi]);
	int key = a[left];
	int cur = left + 1;
	while (cur <= right) {
		if (a[cur] < key) {
			Swap(&a[cur], &a[left]);
			++left;
			++cur;
		}
		else if (a[cur] > key) {
			Swap(&a[cur], &a[right]);
			--right;
		}
		else
			++cur;
	}
	QuickSort(a, begin, left - 1);
	QuickSort(a, right + 1, end);
}

void QuickSortNonR(int* a, int begin, int end) {
	ST st;
	STInit(&st);

	STPush(&st, end);
	STPush(&st, begin);
	while (!STEmpty(&st)) {
		int left = STTop(&st);
		STPop(&st);
		int right = STTop(&st);
		STPop(&st);
		int keyi = PartSort(a, left, right);

		//[left, keyi-1] keyi [keyi+1, right]
		if (keyi + 1 < right) {
			STPush(&st, right);
			STPush(&st, keyi + 1);
		}
		if (left < keyi - 1) {
			STPush(&st, keyi - 1);
			STPush(&st, left);
		}
	}
	STDestroy(&st);
}

//void _MergeSort(int* a, int begin, int end, int* tmp) {
//	if (begin == end)
//		return;
//
//	int mid = (begin + end) / 2;
//	_MergeSort(a, begin, mid, tmp);
//	_MergeSort(a, mid + 1, end, tmp);
//
//	int begin1 = begin, end1 = mid;
//	int begin2 = mid + 1, end2 = end;
//	int i = begin;
//	while (begin1 <= end1 && begin2 <= end2) {
//		if (a[begin1] < a[begin2])
//			tmp[i++] = a[begin1++];
//		else
//			tmp[i++] = a[begin2++];
//	}
//	while (begin1 <= end1)
//		tmp[i++] = a[begin1++];
//	while (begin2 <= end2)
//		tmp[i++] = a[begin2++];
//	memcpy(a + begin, tmp + begin, sizeof(int) * (end - begin + 1));
//}
void _MergeSort(int* a, int begin, int end, int* tmp) {
	if (begin == end)
		return;

	if (end - begin + 1 <= 10) {
		InsertSort(a + begin, end - begin + 1);
		return;
	}

	int mid = (begin + end) / 2;
	_MergeSort(a, begin, mid, tmp);
	_MergeSort(a, mid + 1, end, tmp);

	int begin1 = begin, end1 = mid;
	int begin2 = mid + 1, end2 = end;
	int i = begin;
	while (begin1 <= end1 && begin2 <= end2) {
		if (a[begin1] < a[begin2])
			tmp[i++] = a[begin1++];
		else
			tmp[i++] = a[begin2++];
	}
	while (begin1 <= end1)
		tmp[i++] = a[begin1++];
	while (begin2 <= end2)
		tmp[i++] = a[begin2++];
	memcpy(a + begin, tmp + begin, sizeof(int) * (end - begin + 1));
}
void MergeSort(int* a, int n) {
	int* tmp = (int*)malloc(sizeof(int) * n);
	_MergeSort(a, 0, n - 1, tmp);
	free(tmp);
}

